<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="客户名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="trueName">
        <el-input
          v-model="queryParams.trueName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属行业" prop="industryId">
        <el-input
          v-model="queryParams.industryId"
          placeholder="请输入所属行业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入所属代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机" prop="accountMobile">
        <el-input
          v-model="queryParams.accountMobile"
          placeholder="请输入手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="客户状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择客户状态" clearable>
          <el-option
            v-for="dict in dict.type.business_user_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="试用客户" prop="trial">
        <el-select v-model="queryParams.trial" placeholder="请选择试用客户" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最后活动时间" prop="lastActivityTime">
        <el-date-picker clearable
          v-model="queryParams.lastActivityTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后活动时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后活动IP" prop="ipLastActivity">
        <el-input
          v-model="queryParams.ipLastActivity"
          placeholder="请输入最后活动IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:businessusers:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:businessusers:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:businessusers:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:businessusers:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="businessusersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户ID" align="center" prop="userId" />
      <el-table-column label="客户名称" align="center" prop="companyName" />
      <el-table-column label="真实姓名" align="center" prop="trueName" />
      <el-table-column label="所属行业" align="center" prop="industryId" />
      <el-table-column label="所属代理" align="center" prop="agentId" />
      <el-table-column label="介绍人" align="center" prop="referralUserId" />
      <el-table-column label="手机" align="center" prop="accountMobile" />
      <el-table-column label="邮箱" align="center" prop="accountEmail" />
      <el-table-column label="客户状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_user_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="可用系统" align="center" prop="systemIds" />
      <el-table-column label="试用客户" align="center" prop="trial">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.trial"/>
        </template>
      </el-table-column>
      <el-table-column label="最后活动时间" align="center" prop="lastActivityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastActivityTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后活动IP" align="center" prop="ipLastActivity" />
      <el-table-column label="创建者ID" align="center" prop="createUserId" />
      <el-table-column label="创建时间" align="center" prop="dateCreated" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateCreated, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:businessusers:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:businessusers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="trueName">
          <el-input v-model="form.trueName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="所属行业" prop="industryId">
          <el-input v-model="form.industryId" placeholder="请输入所属行业" />
        </el-form-item>
        <el-form-item label="所属代理" prop="agentId">
          <el-input v-model="form.agentId" placeholder="请输入所属代理" />
        </el-form-item>
        <el-form-item label="介绍人" prop="referralUserId">
          <el-input v-model="form.referralUserId" placeholder="请输入介绍人" />
        </el-form-item>
        <el-form-item label="手机" prop="accountMobile">
          <el-input v-model="form.accountMobile" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="邮箱" prop="accountEmail">
          <el-input v-model="form.accountEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="客户状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择客户状态">
            <el-option
              v-for="dict in dict.type.business_user_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可用系统" prop="systemIds">
          <el-input v-model="form.systemIds" placeholder="请输入可用系统" />
        </el-form-item>
        <el-form-item label="试用客户" prop="trial">
          <el-select v-model="form.trial" placeholder="请选择试用客户">
            <el-option
              v-for="dict in dict.type.yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最后活动时间" prop="lastActivityTime">
          <el-date-picker clearable
            v-model="form.lastActivityTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后活动时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后活动IP" prop="ipLastActivity">
          <el-input v-model="form.ipLastActivity" placeholder="请输入最后活动IP" />
        </el-form-item>
        <el-form-item label="创建者ID" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建者ID" />
        </el-form-item>
        <el-form-item label="创建时间" prop="dateCreated">
          <el-date-picker clearable
            v-model="form.dateCreated"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBusinessusers, getBusinessusers, delBusinessusers, addBusinessusers, updateBusinessusers } from "@/api/system/businessusers";

export default {
  name: "Businessusers",
  dicts: ['business_user_state', 'yes_or_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      businessusersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        trueName: null,
        industryId: null,
        agentId: null,
        accountMobile: null,
        accountEmail: null,
        state: null,
        trial: null,
        lastActivityTime: null,
        ipLastActivity: null,
        createUserId: null,
        dateCreated: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        trueName: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" }
        ],
        accountMobile: [
          { required: true, message: "手机不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "客户状态不能为空", trigger: "change" }
        ],
        trial: [
          { required: true, message: "试用客户不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listBusinessusers(this.queryParams).then(response => {
        this.businessusersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        companyName: null,
        trueName: null,
        industryId: null,
        industryName: null,
        agentId: null,
        referralUserId: null,
        accountMobile: null,
        accountEmail: null,
        state: null,
        systemIds: null,
        trial: null,
        lastActivityTime: null,
        ipLastActivity: null,
        createUserId: null,
        dateCreated: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getBusinessusers(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateBusinessusers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusinessusers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + userIds + '"的数据项？').then(function() {
        return delBusinessusers(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/businessusers/export', {
        ...this.queryParams
      }, `businessusers_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
